package com.group.controller.invoice;
import java.util.List;
import java.text.SimpleDateFormat;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.tag.vo.datatable.SortDirection;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.core.util.MyBeanUtils;

import com.group.entity.invoice.InvoiceEntity;
import com.group.service.invoice.InvoiceServiceI;

/**   
 * @Title: Controller
 * @Description: invoice
 * @author onlineGenerator
 * @date 2018-05-26 21:50:30
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("/invoiceController")
public class InvoiceController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(InvoiceController.class);

	@Autowired
	private InvoiceServiceI invoiceService;
	@Autowired
	private SystemService systemService;
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * invoice列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "invoice")
	public ModelAndView invoice(HttpServletRequest request) {
		return new ModelAndView("com/group/invoice/invoiceList");
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(InvoiceEntity invoice,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(InvoiceEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, invoice, request.getParameterMap());
		try{
			cq.addOrder("createDate", SortDirection.desc);
		//自定义追加查询条件
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		cq.add();
		this.invoiceService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 删除invoice
	 * 
	 * @return
	 */
	@RequestMapping(params = "doDel")
	@ResponseBody
	public AjaxJson doDel(InvoiceEntity invoice, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		invoice = systemService.getEntity(InvoiceEntity.class, invoice.getId());
		message = "invoice删除成功";
		try{
			invoiceService.delete(invoice);
			systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		}catch(Exception e){
			e.printStackTrace();
			message = "invoice删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	
	/**
	 * 批量删除invoice
	 * 
	 * @return
	 */
	 @RequestMapping(params = "doBatchDel")
	@ResponseBody
	public AjaxJson doBatchDel(String ids,HttpServletRequest request){
		AjaxJson j = new AjaxJson();
		message = "invoice删除成功";
		try{
			for(String id:ids.split(",")){
				InvoiceEntity invoice = systemService.getEntity(InvoiceEntity.class, 
				Integer.parseInt(id)
				);
				invoiceService.delete(invoice);
				systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
			}
		}catch(Exception e){
			e.printStackTrace();
			message = "invoice删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}


	/**
	 * 添加invoice
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "doAdd")
	@ResponseBody
	public AjaxJson doAdd(InvoiceEntity invoice, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		message = "invoice添加成功";
		try{
			invoiceService.save(invoice);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}catch(Exception e){
			e.printStackTrace();
			message = "invoice添加失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	
	/**
	 * 更新invoice
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "doUpdate")
	@ResponseBody
	public AjaxJson doUpdate(InvoiceEntity invoice, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		message = "invoice更新成功";
		InvoiceEntity t = invoiceService.get(InvoiceEntity.class, invoice.getId());
		try {
			MyBeanUtils.copyBeanNotNull2Bean(invoice, t);
			invoiceService.saveOrUpdate(t);
			systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
		} catch (Exception e) {
			e.printStackTrace();
			message = "invoice更新失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	

	/**
	 * invoice新增页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "goAdd")
	public ModelAndView goAdd(InvoiceEntity invoice, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(invoice.getId())) {
			invoice = invoiceService.getEntity(InvoiceEntity.class, invoice.getId());
			req.setAttribute("invoicePage", invoice);
		}
		return new ModelAndView("com/group/invoice/invoice-add");
	}
	/**
	 * invoice编辑页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "goUpdate")
	public ModelAndView goUpdate(InvoiceEntity invoice, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(invoice.getId())) {
			invoice = invoiceService.getEntity(InvoiceEntity.class, invoice.getId());
			req.setAttribute("invoicePage", invoice);
		}
		return new ModelAndView("com/group/invoice/invoice-update");
	}
}
